package com.lhd.common.imageaudit.utils;

import com.aliyun.imageaudit20191230.Client;
import com.aliyun.imageaudit20191230.models.*;
import com.aliyun.teautil.models.RuntimeOptions;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @CreateTime: 2023/8/9
 * @Author: <==lhd==>
 * @Description: 图片内容审核

 */
@Getter
@Setter
@Component
@ConfigurationProperties(prefix = "aliyun.imageaudit")
public class ScanImageUtil {
    private String accessKeyId;
    private String accessKeySecret;
    private String endpoint;


    /**
     * 指定图片检测的应用场景
     * - porn：图片智能鉴黄
     * - terrorism：图片敏感内容识别
     * - ad：图片垃圾广告识别
     * - live：图片不良场景识别
     * - logo：图片Logo识别
     */
    String imageScene = "porn,terrorism,ad,live,logo";


    public com.aliyun.imageaudit20191230.Client getScanClient() throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                .setAccessKeyId(accessKeyId)
                .setAccessKeySecret(accessKeySecret);
        config.endpoint = endpoint;
        return new com.aliyun.imageaudit20191230.Client(config);
    }

    public Map doScanImage(List<String> imageUrlList){

        // 创建任务列表
        List<ScanImageRequest.ScanImageRequestTask> taskList = new ArrayList<>();

        // 添加图片路径
        for (String s : imageUrlList) {
            ScanImageRequest.ScanImageRequestTask task = new ScanImageRequest.ScanImageRequestTask();
            task.setImageURL(s);
            task.setDataId(UUID.randomUUID().toString());
            taskList.add(task);
        }

        ScanImageRequest scanImageRequest = new ScanImageRequest()
                .setScene(Arrays.asList(imageScene.split(",")))
                .setTask(taskList);

        RuntimeOptions runtimeOptions = new RuntimeOptions();
        runtimeOptions.setConnectTimeout(3000);
        runtimeOptions.setReadTimeout(6000);

        Map<String, String> resultMap = new HashMap<>();
        try {
            // 获得响应结果
            ScanImageResponse response = getScanClient().scanImageWithOptions(scanImageRequest,runtimeOptions);


            // 处理数据
            final List<ScanImageResponseBody.ScanImageResponseBodyDataResults> results = response.body.data.results;

            for (ScanImageResponseBody.ScanImageResponseBodyDataResults result : results) {
                for (ScanImageResponseBody.ScanImageResponseBodyDataResultsSubResults subResult : result.subResults) {
                    if(!subResult.suggestion.equals("pass")){
                        resultMap.put("suggestion", subResult.suggestion);
                        resultMap.put("label", subResult.label);
                        return resultMap;
                    }
                }
            }



            resultMap.put("suggestion", "pass");
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;



    }
}
